package com.ccx.coachsite.ui.manage;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccx.coachsite.CoachSiteMainActivity;
import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.AnalysisInfo;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.ui.manage.ManageGroupView.GroupAdapter;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerMainBase;

public class ManageCustomInfoView extends OwerBaseView {

	private CustomInfo mApp;
	private List<AnalysisInfo> ltAnalysis;
	private int width;
	
	public ManageCustomInfoView(Context context, OwerMainBase superView, Object cinfo) {
		super(context, superView);
		mApp = (CustomInfo) cinfo;
		loadData();
		width = superView.getWidth();
	}

	protected boolean onLoadData() {
		return  ManageProvider.getInstance().queryCustomInfo(mContext, mApp);
	}

	protected void doRefreshDataSuccess() {
		TextView txt = (TextView)findViewById(R.id.manage_custom_info__detail_name);
		txt.setText(mApp.getName() + "");
		
		TextView txt1 = (TextView)findViewById(R.id.manage_custom_info_sex);
		txt1.setText(mApp.getSex() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_detail_phone);
		txt1.setText(mApp.getTianYi() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_package);
		txt1.setText(mApp.getPackages() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_detail_mode);
		txt1.setText(mApp.getMode() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_package_gprs);
		txt1.setText(mApp.getPackage_gprs() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_os);
		txt1.setText(mApp.getOs() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_baseline);
		txt1.setText(mApp.getBaseLine() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_outpackage);
		txt1.setText(mApp.getOutPackage_gprs() + "");
		
		/*
		txt1 = (TextView)findViewById(R.id.manage_custom_info_last_month_average_gprs);
		txt1.setText(mApp.getLastMonthAverage_gprs() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_state);
		txt1.setText(mApp.getState() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_average_gprs);
		txt1.setText(mApp.getAverage_gprs() + "");
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_detail_recommend);
		txt1.setText(mApp.getRecommend() + "");
		*/
		int pro = mApp.getProportion();
		ProgressBar process = (ProgressBar)findViewById(R.id.manage_custom_info_process_used_gprs);
		process.setProgress(pro);
		
		txt1 = (TextView)findViewById(R.id.manage_custom_info_used_gprs_title);
		txt1.setText("本月累计已使用流量" + mApp.getProportion() + "%");
		
		ltAnalysis = mApp.getAnalysisList();
		if(ltAnalysis == null)
		{
			View anaPanel = findViewById(R.id.manage_custom_anipanel);
			anaPanel.setVisibility(View.GONE);
		}
		else
		{
			GridView grid = (GridView) findViewById(R.id.manage_custom_ani_grid);
			GroupAdapter adapter = new GroupAdapter();
			grid
			.setOnItemClickListener(new OnItemClickListener());
			grid.setAdapter(adapter);
		}
	}
	
	private class OnItemClickListener implements
	android.widget.AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos,
		long arg3) {
			String str = ltAnalysis.get(pos).getName();
			Intent intent = new Intent( CoachSiteMainActivity.ACTION_SEARCH_NAME );
            intent.putExtra("keyword", str);
            mContext.sendBroadcast(intent);
		}
	}
	
	class GroupAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return ltAnalysis.size();
		}

		@Override
		public Object getItem(int position) {
			return ltAnalysis.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = View
					.inflate(mContext, R.layout.manage_custom_anas_item, null);

			view.setBackgroundColor(fetchNextColor());
			
			TextView txtName = (TextView) view
					.findViewById(R.id.manage_custom_anas_name);
			txtName.setText(ltAnalysis.get(position).getName());
			
			return view;
		}

		private int lastColor = -1;

		private int fetchNextColor() {
			if (lastColor == 0xffdc3027)
				lastColor = 0xff57ae47;
			else if (lastColor == 0xff57ae47)
				lastColor = 0xffb63f96;
			else if (lastColor == 0xffb63f96)
				lastColor = 0xff3771b7;
			else
				lastColor = 0xffdc3027;
			return lastColor;
		}
	}
}
